#!/usr/bin/env python
# coding=utf8


import sys

sys.path.append('..')
sys.path.append('../../')
from i_util.pybeanstalk import PyBeanstalk
import json
import traceback

from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.transport.TTransport import TMemoryBuffer
from thrift.protocol.TBinaryProtocol import TBinaryProtocol
from bdp.i_crawler.i_extractor.ttypes import BaseInfo, CrawlInfo, ExtractInfo, PageParseInfo
import time
from pymongo import MongoClient
import hashlib


def main():
    host = '101.201.196.82'
    port = 27017
    database = 'public_data'
    coll = 'shixin_info'
    client = MongoClient(host, port)
    db = client[database][coll]
    min_time = "2016-09-18 00:00:00"
    max_time = "2016-10-18 00:00:00"
    min_ms = time.mktime(time.strptime(min_time, '%Y-%m-%d %H:%M:%S'))
    max_ms = time.mktime(time.strptime(max_time, '%Y-%m-%d %H:%M:%S'))
    print min_ms, max_ms
    param = {"in_time": {"$gt": min_time, "$lt": max_time}}
    cursor = db.find(param)
    num = 0

    try:
        for item in cursor:
            if len(item) > 0:
                num += 1
                src_url = item.get("src_url", "")

                data = get_param_data(item)
                case_id = data.get("case_id", "")
                i_name = data.get("i_name", "")
                data = json.dumps(data)
                site_id = get_md5_i64(item.get("site_id", ""))
                site = src_url.split('//')[-1].split('/')[0]
                base_info = BaseInfo(url=src_url, site_id=site_id, site=site)
                extract_info = ExtractInfo(ex_status=2, extract_data=data, topic_id=38)
                tube = "extract_info"
                send_msg(base_info, extract_info, tube)

            if num % 500 == 0:
                print num
            if num >= 10:
                break
        else:
            print "no result"
    except:
        print traceback.format_exc()

    print "finish sumnum:%s" % num


def get_param_data(item):
    '''获取入参'''
    duty = item.get("duty", "")
    max_money = item.get("max_money", "")
    i_name = item.get("iname", "")
    reg_date = item.get("regDate", "")
    publish_date = item.get("publishDate", "")
    business_entity = item.get("businessEntity", "")
    province = item.get("areaName", "")
    sum_money = item.get("sum_money", "")
    disrupt_type_name = item.get("disrupt_type_name", "")
    case_id = item.get("caseCode", "")
    party_type_name = item.get("partyTypeName", "")
    court = item.get("courtName", "")
    registered_code = item.get("cardNum", "")
    performance = item.get("performance", "")
    gist_unit = item.get("gistUnit", "")
    gist_id = item.get("gistId", "")

    data = {
        "duty": duty,
        "max_money": max_money,
        "i_name": i_name,
        "reg_date": reg_date,
        "publish_date": publish_date,
        "business_entity": business_entity,
        "province": province,
        "sum_money": sum_money,
        "disrupt_type_name": disrupt_type_name,
        "case_id": case_id,
        "party_type_name": party_type_name,
        "court": court,
        "registered_code": registered_code,
        "performance": performance,
        "gist_unit": gist_unit,
        "gist_id": gist_id,

    }
    return data


import ctypes


def get_md5_i64(obj):
    m = hashlib.md5()
    m.update(obj)
    return ctypes.c_int64(int(m.hexdigest()[8:-8], 16)).value


def send_msg(base_info, extract_info, tube):
    '''往消息队列中发送消息'''
    crawl_info = CrawlInfo()

    pybeanstalk = PyBeanstalk('101.201.102.37')

    req = PageParseInfo(base_info=base_info, crawl_info=crawl_info, extract_info=extract_info, scheduler="a",
                        parse_extends="b", data_extends="c")
    tMemory_b = TMemoryBuffer()
    tBinaryProtocol_b = TBinaryProtocol(tMemory_b)
    req.write(tBinaryProtocol_b)
    body = tMemory_b.getvalue()
    try:
        pybeanstalk.put(tube, body)
        # print "send beanstalk msg success"
    except:
        print traceback.format_exc()


if __name__ == "__main__":
    print "start"
    main()
